<script>

    var idCard = "";
    var jkcsUserInfo =StoreCache.getCache("jkcsUserInfo");
    var uri_service_queryTx = '/api/gzd/jrlb/queryTx';


    /**门诊详情*/
    function toMzDetail(obj){
        obj.pid =$.QuickUrlUtils.getRequest("ZXWYH");
        StoreCache.setCache("mzInfo",obj);
        // var url = jkda_view+"/jzjl/mzDetail.html?ZXWYH="+$.QuickUrlUtils.getRequest("ZXWYH")+"&JKDAEID="+EID;
        $.pageSwitch.goToUrl("jkda","jzDetail", "ZXWYH=" + obj.pid);
    }

    /**住院详情*/
    function toZyDetail(data){
        StoreCache.setCache("zyInfo",data);
        var EID = $.JkdaUtils.encryptionStr("fjzlyd-jkda-yddy",data.inHospitalId);
        $.pageSwitch.goToUrl("jkda","zyDetail","ZXWYH="+$.QuickUrlUtils.getRequest("ZXWYH")+"&JKDAEID="+EID);
    }

    var page_mbView = (function($, page_mbView) {


        var cfg_def = {
            queryUserInfo: queryUserInfo,
            indata: indata,
            callBack: callBack,
            getHostList: getHostList,
            appendToZyjl: appendToZyjl,
            appendToMzjl: appendToMzjl,
        }

        page_mbView.init = function(cfg){
            $.extend(page_mbView, cfg_def, cfg);
            // 基本信息展示
            $(".person-info-top").find("span[id]").each(function(){
                var key = $(this).attr("id");
                if(key=="name"){
                    $(this).text(jkcsUserInfo["xm"]);
                }else if(key=="linkmanTel"){
                    $(this).text(jkcsUserInfo["sj"]);
                }else if(key=="age"){
                    var age = new Date().getFullYear() - parseInt(jkcsUserInfo.sfzh.substring(6, 10));
                    $(this).text(age+"岁");
                }else if(key=="gender"){
                    var xb = jkcsUserInfo.xb=="1"?"男":"女"
                    $(this).text(xb);
                }

            })
            if(title=="就诊记录"){
                page_mbView.indata();
            }else if(title=="健康档案"){
                page_mbView.queryUserInfo();
            }

            idCard = $.QuickUrlUtils.getRequest("ZXWYH");
            if(StringUtils.isEmpty(idCard)&&!jkcsUserInfo){
                $.QuickAlert.alertFail({
                    content: "URL参数错误"
                });
            }else{
                if(StringUtils.isEmpty(idCard)&& jkcsUserInfo){
                    idCard = jkcsUserInfo.sfzh;
                }else{
                    idCard = $.JkdaUtils.getIdCard();
                }
                if(!StringUtils.isEmpty(idCard)){
                    $.QuickRemote.AjaxJson({
                        funcName:uri_service_queryTx
                        ,callback:function(result) {
                            var img = result.src;
                            if(img){
                                if(img.indexOf("base64")=="-1")
                                    img= "data:image/jpeg;base64,"+img;
                                $("#userHeader").attr("src",img);
                            }else{
                                //  $("#userHeader").attr("src",img);
                            }
                        },params:{txid: jkcsUserInfo.txid}
                    });

                }
            }

        }

        return page_mbView;
        function queryUserInfo(){
            var userInfo = StoreCache.getCache("jkcsUserInfo");
            if(!userInfo.sfzh){
                $.QuickAlert.alertConfirm({
                    content: "未查到用户信息，请在家人列表补充本人信息！", btn: ["现在就去", "以后再说"], callback: function () {
                        $.pageSwitch.goToUrl("family","familyAdd");
                    }
                });
            }else{
                var data = {
                    // accessType:"1",
                    idCard:userInfo.sfzh,
                    // cardType:"01",
                    passWord:userInfo.sfzh.substr(userInfo.sfzh.length-6)
                }
                $.QuickRemote.AjaxJson({
                    funcName:"/api/gzd/jkda/queryJkdaUserInfo"
                    ,callback: function(result){
                        if(!result.zxwyh){
                            $.QuickAlert.alertNormal("不存在此人档案!", {
                                callback: function(){
                                    $.QuickUrlUtils.reBackAndRefreshJSBridge();
                                }
                            });
                        }
                        else {
                            // $("body").data("jkdaUserInfo", result);
                            $(".person-info-top").find("span[id]").each(function(){
                                var key = $(this).attr("id");
                                var value = result[key];
                                if(key=="age"){
                                    $(this).text(value+"岁");
                                }else{
                                    $(this).text(value);
                                }
                                if (result && result["zxwyh"]!="") {
                                    $("body").on("click", ".runDiv", function(){
                                        var url = $(this).attr("url");
                                        if(!url){
                                            $.QuickAlert.alertFail({
                                                content: "改模块暂未开放"
                                            });
                                            return false;
                                        }
                                        else{
                                            $.pageSwitch.goToUrl("jkda",url,"ZXWYH="+result["zxwyh"]);
                                        }

                                    });
                                }

                            })
                        }
                    }
                    ,params:data
                })
            }

        }
        function indata(){
            //查询就诊记录
            var zxwyh =$.QuickUrlUtils.getRequest("ZXWYH");
            $.QuickRemote.AjaxJson({
                funcName: "/api/gzd/jkda/queryJzjl"
                ,callback:page_mbView.callBack
                ,params:{"zxwyh": zxwyh,
                    "currentPage": "1",
                    "pageSize": "10",}
            });
        }

        //就诊记录回调
        function callBack(result){
            jzjl = result;
            page_mbView.appendToMzjl(result);
            var mzjl = $.grep(result, function(num,index){
                if(num["BGLX"] == "C0001.0201"){
                    return num;
                }
            });
            var zyjl = $.grep(result, function(num,index){
                if(num["BGLX"] == "C0002.0201"){
                    return num;
                }
            });
            $("#ListTab li").eq(0).click(function(){
                page_mbView.appendToMzjl(result);
            }).click();
            $("#ListTab li").eq(1).click(function(){
                page_mbView.getHostList();
            });
        }
        function getHostList(){
            var zxwyh =$.QuickUrlUtils.getRequest("ZXWYH");
            $.QuickRemote.AjaxJson({
                funcName:"/api/gzd/jkda/queryZys"
                ,callback:page_mbView.appendToZyjl
                ,params:{zxwyh: zxwyh,currentPage:"1",pageSize:"10"}
            });
        }
        /***住院记录***/
        function appendToZyjl(result){
            result= result;
            $("#zyjl").empty();
            var trs = [];
            if(result && result.length > 0){
                var data = template("zyjlTpl", {data:result});
                $("#zyjl").html(data);
            }else{
                $("#zyjl").append("<div style='width:100%;text-align:center; font-weight:bold;color:#000;padding-top:20px'>暂无信息</div>");
            }

        }

        /***门诊记录***/
        function appendToMzjl(result){
            $("#mzjl").empty();
            var trs = [];
            if(result && result.length > 0){
                var data = template("mzjlTpl", {data:result});
                $("#mzjl").html(data);
            }else{

                $("#mzjl").append("<div style='width:100%;text-align:center; font-weight:bold;color:#000;padding-top:20px'>暂无信息</div>");
            }

        }
    })(jQuery, {});
    
</script>